import zlib
from typing import Literal

from utils.MiCryptoKey import m_iCryptoKey
from utils.创建文件夹 import createFolder

file_path = "output/1_res"
file_names = ["CARD_Desc", "CARD_Indx", "CARD_Name"]
file_names.extend(["CARD_Part", "CARD_Pidx"])


def 解密(file_name, lang=Literal["en", "jp"]):
    with open(f"{file_path}/{lang}/{file_name}", "rb") as f:
        data = bytearray(f.read())

    for i in range(len(data)):
        v = i + m_iCryptoKey + 0x23D
        v *= m_iCryptoKey
        v ^= i % 7
        data[i] ^= v & 0xFF

    with open(f"output/2_res/{lang}/{file_name}" + ".txt", "wb") as f:
        f.write(zlib.decompress(data))


createFolder("output/1_res/en")
createFolder("output/1_res/jp")

for lang in ["en", "jp"]:
    for name in file_names:
        解密(name, lang)
